/**
 * dailyAdmin vuex
 */
import posts from '@/plugins/request'

// initial state
const state = {
  eventAdmin: {
    data: [],
    total: 0,
    imgList: []
  },
  logAdmin: {
    data: [],
    total: 0
  },
  taskAdmin: {
    data: [],
    total: 0,
  }
}
// getters
const getters = {
  getEventAdmin: state => state.eventAdmin,
  getLogAdmin: state => state.logAdmin,
  getTaskAdmin: state => state.taskAdmin,
}
// actions
const actions = {
  async getEventAdminListData({commit}, param) {
    let data = await posts.postDailyEventAdminList(param)
    // debugger
    data.rows.map(v => {
      if(v.path) {
        v.path = v.path.split(',')
        v.path.pop()
        // 尝试图片预加载
        v.imgList = v.path.map(imgUrl => {
          let image = new Image();
          image.src = 'http://47.112.16.168:' + imgUrl
          return image
        })
      }
    })
    // debugger
    commit('changeEventAdming', data)
  },
  async uplaodEventAdmin({commit}, formdata) {
    let res = await posts.postDailyEventAdminUpload(formdata)
    return res
  },
  async getLogAdminListData({commit}, param) {
    let data = await posts.postDailyLogAdminList(param)
    commit('changeLogAdmin', data)
  }, 
  async getDailyTaskList({commit}, param) {
    let data = await posts.getDailyTaskList(param)
    commit('changeTaskAdmin', data)
  }, 
}

// mutations
const mutations = {
  changeEventAdming(state, data) {
    state.eventAdmin.data = data.rows
    state.eventAdmin.total = data.total
  },
  changeLogAdmin(state, data) {
    state.logAdmin.data = data.rows
    state.logAdmin.total = data.total
  },
  changeTaskAdmin(state, data) {
    state.taskAdmin.data = data.rows
    state.taskAdmin.total = data.total
  },
}
export default {
  state,
  getters,
  actions,
  mutations
}